<template>
  <div>
    <div class="dialog_category">
      <el-dialog ref="dislogRef" append-to-body :visible.sync="dialogVisible" width="80%" height="80vh">
        <div slot="title">
          <div class="title_span">服务与承诺</div>
          <a-button type="primary" @click="handleAdd">新增</a-button>
        </div>
        <a-card :bordered="false">
          <div class="table-operator">
            <!-- <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button> -->
          </div>
          <a-table
            v-if="!isLoading"
            rowKey="serviceId"
            :columns="columns"
            :dataSource="serviceList"
            :pagination="false"
            :loading="isLoading"
            :scroll="{ y: 500 }"
          >
            <span slot="status" slot-scope="text">
              <a-tag :color="text == 1 ? 'green' : ''">{{ text == 1 ? '显示' : '隐藏' }}</a-tag>
            </span>
            <!-- 是否默认 -->
            <span slot="isDefault" slot-scope="text">
              <a-tag :color="text == 1 ? 'green' : ''">{{ text == 1 ? '是' : '否' }}</a-tag>
            </span>
            <span slot="action" slot-scope="text, item">
              <a style="margin-right: 8px" @click="handleEdit(item)">编辑</a>
              <a @click="handleDelete(item)">删除</a>
            </span>
          </a-table>
        </a-card>
      </el-dialog>
    </div>
    <div>
      <el-dialog width="30%" title="提示" :visible.sync="innerVisible" append-to-body>
        <span>确认要删除该记录吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="onYes">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <dialogAddService ref="AddForm" :categoryList="serviceList" @handleSubmit="handleRefresh" />
  </div>
</template>

<script>
import { getAction, putAction, deleteAction, downPostFile, postAction } from '@/api/manage'

import { ACCOUNT_BOOK } from '@/store/mutation-types'
import Vue from 'vue'
import dialogAddService from './dialogAddService'

export default {
  name: 'Index',
  components: {
    dialogAddService,
  },
  data() {
    return {
      // 账套id
      accountBookId: Vue.ls.get(ACCOUNT_BOOK).id,
      dialogVisible: false,
      innerVisible: false,
      deleteId: '', //删除id
      // 分类列表
      serviceList: [],
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '服务名称',
          dataIndex: 'name',
        },
        {
          title: '概述',
          dataIndex: 'summary',
        },
        {
          title: '是否默认',
          dataIndex: 'isDefault',
          scopedSlots: { customRender: 'isDefault' },
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '排序',
          dataIndex: 'sort',
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },
  created() {
    // 获取分类列表
    this.getCategoryList(true)
  },
  methods: {
    // 获取分类列表
    getCategoryList(bool) {
      bool && (this.isLoading = true)
      getAction('/goods/yoshopGoodsService/list', { storeId: this.accountBookId, pageNo: 1, pageSize: 1000 })
        .then((result) => {
          console.log(result, 'result')
          this.serviceList = result.result.records
        })
        .finally((result) => {
          this.isLoading = false
        })
    },

    // 新增记录
    handleAdd() {
      this.$refs.AddForm.add()
    },

    // 编辑记录
    handleEdit(item) {
      this.$refs.AddForm.add(item)
    },

    // 删除记录
    handleDelete(item) {
      console.log(item, 'item', item['serviceId'])
      this.innerVisible = true
      this.deleteId = item['serviceId']
    },
    onYes() {
      deleteAction('/goods/yoshopGoodsService/delete', {
        id: this.deleteId, //多个商品id用逗号分割
      }).then((res) => {
        if (res.code === 200) {
          this.$elMessage.success(res.message)
          this.handleRefresh()
        } else {
          this.$elMessage.error(res.message)
        }
      })
      this.innerVisible = false
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh() {
      // 获取分类列表
      this.getCategoryList()
    },
  },
}
</script>
<style lang="scss" scoped>
.dialog_category {
  ::v-deep .el-dialog__header {
    border-bottom: none;
    .title_span {
      font-size: 14px;
    }
    span {
      font-weight: 400 !important;
      font-size: 14px !important;
      color: #fff !important;
    }
    .ant-btn {
      width: 80px;
      color: #fff;
      background: #1993fb;
      border-radius: 4px 4px 4px 4px;
      margin-top: 10px;
    }
  }
  ::v-deep .el-dialog__body {
    padding: 0;
  }
}
::v-deep .ant-btn {
  width: 80px;
  color: #fff;
  background: #1993fb;
  border-radius: 4px 4px 4px 4px;
  margin-top: 10px;
  span {
    font-weight: 400 !important;
    font-size: 14px !important;
    color: #fff !important;
  }
}
</style>
